package frontend.admin;

import frontend.Login;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class LabelManagement extends JFrame {
    private JPanel centerPanel;
    private static final String[] labels = {"用户管理", "疾病管理"};
    private final Color SELECTED_COLOR = new Color(255, 255, 255);
    private final Color UNSELECTED_COLOR = new Color(245, 245, 245);
    private JPanel labelPanel;

    public LabelManagement() {
        setTitle("管理员界面");
        setSize(1500, 800);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(new BorderLayout());

        // 顶部标签面板
        createTopPanel();

        // 中间内容区域
        centerPanel = new JPanel(new BorderLayout());
        centerPanel.setBackground(Color.WHITE);
        add(centerPanel, BorderLayout.CENTER);

        // 底部退出按钮
        add(createBottomPanel(), BorderLayout.SOUTH);

        // 默认选中第一个标签
        selectFirstTab();
    }

    private void createTopPanel() {
        JPanel topPanel = new JPanel(new BorderLayout(20, 0));
        topPanel.setBackground(UNSELECTED_COLOR);
        topPanel.setBorder(BorderFactory.createEmptyBorder(15, 0, 0, 10));

        labelPanel = new JPanel(new GridLayout(1, labels.length));
        labelPanel.setOpaque(true);

        for (String label : labels) {
            labelPanel.add(createClickableLabel(label));
        }

        topPanel.add(labelPanel, BorderLayout.CENTER);
        add(topPanel, BorderLayout.NORTH);
    }

    private JLabel createClickableLabel(String text) {
        JLabel label = new JLabel(text);
        label.setFont(new Font("微软雅黑", Font.BOLD, 20));
        label.setOpaque(true);
        label.setBackground(UNSELECTED_COLOR);
        label.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        label.setForeground(Color.DARK_GRAY);

        label.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                updateLabelColors(label);
                handleLabelClick(text);
            }
        });

        return label;
    }

    private void updateLabelColors(JLabel selectedLabel) {
        // 重置所有标签颜色
        for (Component comp : labelPanel.getComponents()) {
            if (comp instanceof JLabel) {
                comp.setBackground(UNSELECTED_COLOR);
            }
        }
        // 设置选中标签样式
        selectedLabel.setBackground(SELECTED_COLOR);
    }

    private void selectFirstTab() {
        if (labelPanel.getComponentCount() > 0) {
            JLabel firstLabel = (JLabel) labelPanel.getComponent(0);
            updateLabelColors(firstLabel);
            handleLabelClick(labels[0]);
        }
    }

    private void handleLabelClick(String labelText) {
        centerPanel.removeAll();

        if ("用户管理".equals(labelText)) {
            centerPanel.add(new DataManagementPanel("user"), BorderLayout.CENTER);
        } else if ("疾病管理".equals(labelText)) {
            centerPanel.add(new DataManagementPanel("disease"), BorderLayout.CENTER);
        }

        centerPanel.revalidate();
        centerPanel.repaint();
    }

    private JPanel createBottomPanel() {
        JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 20, 10));
        JButton logoutButton = new JButton("退出登录");
        logoutButton.setFont(new Font("微软雅黑", Font.BOLD, 14));
        logoutButton.setBackground(new Color(255, 99, 71));
        logoutButton.setForeground(Color.WHITE);
        logoutButton.setFocusPainted(false);

        logoutButton.addActionListener(e -> {
            if (JOptionPane.showConfirmDialog(this, "确定要退出登录吗？", "退出确认",
                    JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                dispose();
                SwingUtilities.invokeLater(() -> {
                    try {
                        new Login();
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                });
            }
        });

        bottomPanel.add(logoutButton);
        return bottomPanel;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new LabelManagement().setVisible(true));
    }
}
